package cn.skcks.springboot05mybatis.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.function.Predicate;

@Configuration
//@EnableSwagger2 // Swagger2
@EnableOpenApi // Swagger3
public class SwaggerConfig {

//	@SuppressWarnings("rawtypes")
	private ApiInfo apiInfo() {
		// 作者信息
		Contact contact = new Contact("时空", "http://github.com/shikong-sk", "");
		return new ApiInfo("xxx Api 文档",
				"xxx 文档描述",
				"v1.0",
				"#",
				contact,
				"Apache 2.0",
				"http://www.apache.org/licenses/LICENSE-2.0",
//				new ArrayList<VendorExtension>() {{
//					ObjectVendorExtension objectVendorExtension = new ObjectVendorExtension("x-logo");
//					objectVendorExtension.addProperty(new StringVendorExtension("author", "Shikong"));
//					add(objectVendorExtension);
//				}});
				new ArrayList<>());
	}

	// 配置 Swagger 的 Docket 实例
	@Bean
	public Docket docket(Environment environment) {

		// 配置要显示的环境
		Profiles profiles = Profiles.of("dev","test");

		boolean enable = (environment.acceptsProfiles(profiles));

		return new Docket(DocumentationType.OAS_30)
				.apiInfo(apiInfo())
				.enable(enable) // 是否启动 Swagger
				.groupName("Mybatis")
				.select()
//				.apis(RequestHandlerSelectors // 指定要扫描接口的方式
//						// 指定扫描的包名
//						.basePackage("cn.skcks.springboot05mybatis.controller")
//				)
//				.apis(RequestHandlerSelectors.any()) // 扫描全部包
				.apis(RequestHandlerSelectors.basePackage("cn.skcks"))
//				.paths(PathSelectors.ant("cn/**")) // 指定路径
				.build();
	}

	@Bean
	public Docket docket2(){
		return new Docket(DocumentationType.OAS_30)
				.groupName("默认模板")
				.select()
				.apis(RequestHandlerSelectors.basePackage("org.springframework.boot.autoconfigure.web"))
				.build();
	}
}